// 
// 需要通过消息总线传递的参数的一些序列化和反序列化的模板定义
// 
#ifndef WWBIZ_PARAMDEF_TEMPLATE_H
#define WWBIZ_PARAMDEF_TEMPLATE_H
#include "../../Core/msgbus_interface.h"
#include "../../Core/xparam.hpp"
#include <map>
#include <string>
#include <vector>

using namespace NetMsgBus;

namespace wwbiz
{
    class ParamBase
    {
    public:
        virtual void FromXParam(const core::XParam& xp) = 0;
        virtual core::XParam ToXParam() const = 0;
        virtual ~ParamBase(){}
    };
    template <typename T> class ParamDefTemplate : public ParamBase
    {
    public:
        virtual ~ParamDefTemplate(){}
        void FromMsgBusParam(MsgBusParam param)
        {
            core::XParam xp;
            Param2CustomType(param, xp);
            FromXParam(xp);
        }
        MsgBusParam ToMsgBusParam() const
        {
            return CustomType2Param(ToXParam());
        }
        static void XParamToVectorType(const core::XParam& xp, std::vector<T>& result)
        {
            result.clear();
            std::vector<core::XParam> xpvec;
            xp.get_XParamVector("vectypeparam", xpvec);
            for(size_t i = 0; i < xpvec.size(); i++)
            {
                T one;
                one.FromXParam(xpvec[i]);
                result.push_back(one);
            }
        }
        static core::XParam VectorTypeToXParam(const std::vector<T>& src)
        {
            std::vector<core::XParam> xpvec;
            for(size_t i = 0; i < src.size(); i++)
            {
                xpvec.push_back(src[i].ToXParam());
            }
            core::XParam xp;
            xp.put_XParamVector("vectypeparam", xpvec);
            return xp;
        }
        static void MsgBusParamToVectorType(MsgBusParam param, std::vector<T>& result)
        {
            core::XParam xp;
            Param2CustomType(param, xp);
            XParamToVectorType(xp, result);
        }
        static MsgBusParam VectorTypeToMsgBusParam(const std::vector<T>& src)
        {
            return CustomType2Param(VectorTypeToXParam(src));
        }
        static void XParamToMapType(const core::XParam& xp, std::map<std::string, T>& result)
        {
            result.clear();
            std::map<std::string, core::XParam> xpmap;
            xp.get_XParamMap("maptypeparam", xpmap);
            std::map<std::string, core::XParam>::const_iterator cit = xpmap.begin();
            while(cit != xpmap.end())
            {
                T one;
                one.FromXParam(cit->second);
                result[cit->first] = one;
                ++cit;
            }
        }
        static core::XParam MapTypeToXParam(const std::map<std::string, T>& src)
        {
            std::map<std::string, core::XParam> xpmap;
            typename std::map<std::string, T>::const_iterator cit = src.begin();
            while(cit != src.end())
            {
                xpmap[cit->first] = cit->second.ToXParam();
                ++cit;
            }
            core::XParam xp;
            xp.put_XParamMap("maptypeparam", xpmap);
            return xp;
        }
        static void MsgBusParamToMapType(MsgBusParam param, std::map<std::string, T>& result)
        {
            core::XParam xp;
            Param2CustomType(param, xp);
            XParamToMapType(xp, result);
        }
        static MsgBusParam MapTypeToMsgBusParam(const std::map<std::string, T>& src)
        {
            return CustomType2Param(MapTypeToXParam(src));
        }
    };
}

#endif
